<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:bill:export']"
        >导出</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="el-form-search">
                        <el-form-item label="房间号" prop="roomNo" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.roomNo"
                                    placeholder="请输入房间号"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="用户名" prop="name" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.name"
                                    placeholder="请输入用户名"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="审核" prop="audit" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.audit"
                                    placeholder="请输入是否需要审核"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
            <el-form-item class="el-form-search-item">
                <el-button type="primary" icon="el-icon-search" size="m    ini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="#" type="index" width="50" align="center">
            <template scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
            </template>
        </el-table-column>
      <el-table-column label="订单id" align="center" prop="billId" width="180"/>
      <el-table-column label="房间号" align="center" prop="roomNo" />
      <el-table-column label="用户名" align="center" prop="name" />
      <el-table-column label="预定时间" align="center" prop="billTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入住时间" align="center" prop="checkInTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkInTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退房时间" align="center" prop="checkOutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkOutTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核" align="center" prop="audit"
                       :formatter="auditFormatter"
      />

      <el-table-column label="订单状态" align="center" prop="billStatus"
                       :formatter="billStatusFormatter"
      />

      <el-table-column label="校区id" align="center" prop="campusId" :show-tooltip-when-overflow="true"/>
      <el-table-column label="备注" align="center" prop="remark" :show-tooltip-when-overflow="true"/>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bill:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bill:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="房间号" prop="roomNo">
          <el-select
              v-model="form.roomNo"
              placeholder="请输入房间号"
              clearable
              :style="{ width: '100%' }"
          >
            <el-option
                v-for="item in allRoomNo"
                :key="item.index"
                :label="item.roomNo"
                :value="item.roomNo"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="预定时间" prop="billTime">
          <el-date-picker clearable size="mini"
            v-model="form.billTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择预定时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入住时间" prop="checkInTime">
          <el-date-picker clearable size="mini"
            v-model="form.checkInTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择入住时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退房时间" prop="checkOutTime">
          <el-date-picker clearable size="mini"
            v-model="form.checkOutTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择退房时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="审核" prop="audit">
          <el-select
              v-model="form.audit"
              placeholder="请输入是否需要审核"
              clearable
              :style="{ width: '100%' }"
          >
            <el-option
                v-for="(item, index) in auditOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select
              v-model="form.billStatus"
              placeholder="请选择订单状态"
              clearable
              :style="{ width: '100%' }"
          >
            <el-option
                v-for="(item, index) in billStatusOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="校区id" prop="campusId" >
          <el-input v-model="form.campusId" placeholder="请输入校区id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBill, getBill, delBill, addBill, updateBill } from "@/api/system/bill";

export default {
  name: "Bill",
  dicts: ['bill_status'],
  data() {
    return {
      allRoomNo:[{
        roomNo:'',
        index:'',
      }],
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约管理表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "create_time",
        isAsc: "desc",
        roomNo: null,
        name: null,
        audit: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billId: [
          { required: true, message: "订单id不能为空", trigger: "blur" }
        ],
        audit: [
          { required: true, message: "是否需要审核不能为空", trigger: "blur" }
        ],
      },
      auditOptions:[
          {
            label: "需要",
            value: "1",
          },{
            label: "不需要",
            value: "0",
        }
      ],
      billStatusOptions:[
        {
          label: "不通过",
          value: "0",
        },{
          label: "预约中",
          value: "1",
        },{
          label: "入住中",
          value: "2",
        },{
          label: "已退房",
          value: "3",
        },{
          label: "已撤销",
          value: "4",
        }
      ]
    };
  },
  created() {
    this.getList();
    this.loadAllRoomNo();
  },
  methods: {
    loadAllRoomNo(){
      this.allRoomNo=[];
      this.axios({
        method:'GET',
        url:'http://localhost:8088/system/room/getAllRoomNo',
      }).then((res)=>{
          let tmp = res.data.data;
          for(let i = 0 ; i<tmp.length ; i++){
            this.allRoomNo.push({roomNo:tmp[i], index: i})
          }
      })
    },
      billStatusFormatter(row, value) {
        if (row.billStatus === "0"){
          return <el-tag type="danger">不通过</el-tag>;
        }else if(row.billStatus === "1"){
          return <el-tag type="primary">预约中</el-tag>;
        }else if(row.billStatus === "2"){
          return <el-tag type="success">入住中</el-tag>;
        }else if(row.billStatus === "3"){
          return <el-tag type="info">已退房</el-tag>;
        }else if(row.billStatus === "4"){
          return <el-tag type="warning">已撤销</el-tag>;
        }
},
    auditFormatter(row, value) {
        if (row.audit === "0"){
          return <span >不需要</span>;
        }else if(row.audit === "1"){
          return <span >需要</span>;
        }
},
    /** 查询预约管理列表 */
    getList() {
      this.loading = true;
      listBill(this.queryParams).then(response => {
        this.billList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        roomNo: null,
        name: null,
        billTime: null,
        checkInTime: null,
        checkOutTime: null,
        audit: null,
        billStatus: "0",
        campusId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.billId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const billId = row.billId || this.ids
      getBill(billId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.billId != null) {
            updateBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function() {
        return delBill(billIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bill/export', {
        ...this.queryParams
      }, `bill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
